package feedback.web.bean.admin;

import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;

import org.springframework.beans.factory.annotation.Autowired;

import feedback.core.models.entities.Faculty;
import feedback.core.services.FacultyServices;
import feedback.web.util.FacesUtil;

@ManagedBean(name="fcb")
@SessionScoped
public class FacultyBean {
@ManagedProperty("#{fcsv}")
private FacultyServices services;
@Autowired
private List<Faculty> faculties ;
@Autowired
private List<Faculty> select;
@Autowired
private Faculty add = new Faculty() ;

/**
 * Edit
 * */
private Faculty selectEdit = new Faculty();
private Faculty selectEditTmp = new  Faculty();

@PostConstruct
public void init(){
	try {
		faculties = (List<Faculty>) services.queryAll(Faculty.class);
	} catch (Exception e) {
		
	}
}
public void deleteFaculty(){
	try {
		if(select.size()==0){
			info("", "Please chosse one row befor click delete button!");
			return;
		}
		for(Faculty f: select){
			services.delete(f);
		}
		faculties = (List<Faculty>) services.queryAll(Faculty.class);
			
	} catch (Exception e) { 	
		// TODO: handle exception
	}
	info("", "Delete complite!");
}
public void add(){
	
		try {
			if(validateNo()){
			Faculty tmp = services.save(add);
			if(tmp.getFacultyId()!=0){
				info("", "Faculty " + tmp.getFacultyNo() +" is add to data.");
			}else{
				info("Sorry", "We can add faculty " + add.getFacultyNo()+  ". Please try again later!" );
			}
			setAdd(new Faculty());
			faculties = (List<Faculty>) services.queryAll(Faculty.class);
			}
		} catch (Exception e) {
			info("Sorry", "We can add faculty " + add.getFacultyNo()+  ". Please try again later!" );
		}
		
	
}
public String edit(){
	try {
		if(select.size()==0){
			info("", "Please chosse more one row befor click delete button!");
			return null;
			
		}
		setSelectEdit(select.get(0));
		loadEditTmp();
		return "edit-faculty";	
	} catch (Exception e) { 	
		// TODO: handle exception
	}
	return null;
}
public void update(){
	if((selectEdit.getFacultyNo().equals(selectEditTmp.getFacultyNo()))&&(selectEdit.getFacultyName().equals(selectEditTmp.getFacultyName()))){
		warn("", "Data in form not change!");
		return;
	}else{
		if(selectEditTmp.getFacultyNo().trim().equals("")){
			warn("", "Please enter value Code ID!");
		}else
		if(selectEditTmp.getFacultyName().trim().equals("")){
				warn("", "Please enter value Code Name!");
		}else{
			
			try {
				Faculty f = services.getByNo(selectEditTmp.getFacultyNo());
				if(f!=null){
				if(f.getFacultyId()!=selectEdit.getFacultyId()){
					error("", selectEdit.getFacultyNo()+ " Has been Database!");
				return;
				}
				}
				selectEdit.setFacultyNo(selectEditTmp.getFacultyNo());
				selectEdit.setFacultyName(selectEditTmp.getFacultyName());
				services.update(selectEdit);
				info("", selectEdit.getFacultyNo()+ " updated!");
			} catch (Exception e) {
				error("", selectEdit.getFacultyNo()+ " Has been Database!");
			}	
		}
		
	}

}
public void loadEditTmp() {
	Faculty f =new Faculty();
	f.setFacultyNo(selectEdit.getFacultyNo());
	f.setFacultyName(selectEdit.getFacultyName());
	setSelectEditTmp(f);
	
}
public boolean validateNo(){
	Faculty tmp = services.getByNo(add.getFacultyNo());
	if(tmp.getFacultyId()!=0){
		warn("", "Faculyt code: " + add.getFacultyNo()+" is exist!");
		return false;
	}
	return true;
}
public void info(String title, String detail) {
	FacesUtil.getFacesContext().addMessage(null,
			new FacesMessage(FacesMessage.SEVERITY_INFO, title, detail));
}

public void warn(String title, String detail) {
	FacesUtil.getFacesContext().addMessage(null,
			new FacesMessage(FacesMessage.SEVERITY_WARN, title, detail));
}

public void error(String title, String detail) {
	FacesUtil.getFacesContext().addMessage(null,
			new FacesMessage(FacesMessage.SEVERITY_ERROR, title, detail));
}

public void fatal(String title, String detail) {
	FacesUtil.getFacesContext().addMessage(null,
			new FacesMessage(FacesMessage.SEVERITY_FATAL, title, detail));
}
public List<Faculty> getFaculties() {
	return faculties;
}
public void setFaculties(List<Faculty> faculties) {
	this.faculties = faculties;
}
public List<Faculty> getSelect() {
	return select;
}
public void setSelect(List<Faculty> select) {
	this.select = select;
}
public Faculty getAdd() {
	return add;
}
public void setAdd(Faculty add) {
	this.add = add;
}
public FacultyServices getServices() {
	return services;
}
public void setServices(FacultyServices services) {
	this.services = services;
}
public Faculty getSelectEdit() {
	return selectEdit;
}
public void setSelectEdit(Faculty selectEdit) {
	this.selectEdit = selectEdit;
}
public Faculty getSelectEditTmp() {
	return selectEditTmp;
}
public void setSelectEditTmp(Faculty selectEditTmp) {
	this.selectEditTmp = selectEditTmp;
}






}
